#!/bin/zsh

# Describe:     install intel homebrew
# Create Date： 2021-11-04
# Create Time:  17:-05
# Update Date:  2021-11-04
# Update Time:  19:24
# Author:       MiaoCunFa
# Version:      v0.0.4
#
# Usage:        /bin/zsh -c "$(curl -fsSL https://gitee.com/miaocunfa/mac/raw/master/intel_brew.sh)"
#

#===================================================================

download=$HOME/Downloads
homebrew="Homebrew-intel-20211104.tar"
r_md5="fa4f591abdfec11ae769f5fd5be7d930"

#-------------------------------------------------------------------

function __download()
{
    echo -e "\t开始下载: Homebrew"
    curl -o $download/$homebrew -O http://gjr-soft-repo.oss-cn-qingdao.aliyuncs.com/mac/intel/$homebrew
}

function __md5()
{
    echo -e "\t检测到本地已经存在安装包: $download/$homebrew"
    echo -e "\t对本地安装包进行md5校验: "
    echo -e "\t远端 md5值为: $r_md5"

    l_md5=$(/sbin/md5 $download/$homebrew | awk '{print $4}')
    echo -e "\t本地 md5值为: $l_md5"

    if [ "$r_md5" = "$l_md5" ]
    then
        echo -e "\t本地下载包与远端一致, 继续处理"
    else
        echo -e "\t本地下载包与远端不一致, 删除本地包"
        rm -f $download/$homebrew

        echo -e "\t重新下载: Homebrew"
        download
    fi
}

#-------------------------------------------------------------------
# homebrew

if [ ! -f "$download/$homebrew" ]
then
    echo
    __download
else
    __md5
fi

echo
echo -e "\t开始安装: Homebrew"

echo -e "\t请输入密码 以在/usr/local目录解压homebrew"
cd $download
sudo tar -xvf $homebrew -C /usr/local

if [ $? -ne 0 ]
then
    echo "检测到安装包未能正确安装，程序退出!"
    exit 0
fi

echo -e "\t请输入密码 以修改/usr/local下的文件权限"
cd /usr/local
whoami=$(who am i | awk '{print $1}')
sudo chown -R ${whoami}:admin ./*

echo
echo -e "\tHomebrew 安装已完成"
echo -e "\t输入brew 命令开始查看吧!"


